package org.code.mallspringboot.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.code.mallspringboot.mapper.MemberMapper;
import org.code.mallspringboot.pojo.SmsCoupon;
import org.code.mallspringboot.pojo.SmsCouponHistory;
import org.code.mallspringboot.pojo.UmsMemberReceiveAddress;
import org.code.mallspringboot.service.SmsCouponHistoryService;
import org.code.mallspringboot.service.SmsCouponProductRelationService;
import org.code.mallspringboot.service.SmsCouponService;
import org.code.mallspringboot.service.UmsMemberReceiveAddressService;
import org.code.mallspringboot.utils.JwtUtil;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/member")
public class CouponController {


    @Autowired
    private SmsCouponService smsCouponService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SmsCouponHistoryService smsCouponHistoryService;

    @Autowired
    private SmsCouponProductRelationService smsCouponProductRelationService;

    @Autowired
    private UmsMemberReceiveAddressService umsMemberReceiveAddressService;
    @GetMapping("/coupon/listByProduct/{productId}")
    public CommonResult fetchProductCouponList(@PathVariable int productId){
        List<SmsCoupon> list = smsCouponService.listByProduct(productId);
//        if (!list.isEmpty()){
            return CommonResult.success(list);
//        }
//        return CommonResult.failed("领取优惠券失败！");
    }

    @PostMapping("/coupon/add/{couponId}")
    public CommonResult addMemberCoupon(@PathVariable int couponId, HttpServletRequest request){
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 16; i++){
            int coupon_code = random.nextInt(10);
            stringBuilder.append(coupon_code);
        }
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        String member_nickname = userName;
        int member_id = memberMapper.member_id(member_nickname);
        int couponId2 = smsCouponProductRelationService.couponId(couponId);
        SmsCouponHistory smsCouponHistory = new SmsCouponHistory();
        smsCouponHistory.setCouponId(couponId2);
        smsCouponHistory.setMemberId(member_id);
        smsCouponHistory.setCouponCode(String.valueOf(stringBuilder));
        smsCouponHistory.setMemberNickname(member_nickname);
        if (smsCouponHistoryService.add(smsCouponHistory) > 0){
            if (smsCouponService.update_count(couponId2) > 0) {//优惠券-1
                System.out.println("sms_coupon表优惠券-1");
            }
            return CommonResult.success("sms_coupon_history表新增成功!");
        }
        return CommonResult.failed("领取优惠券失败!");
    }

    /**
        收货地址展示
     * @param request
     * @return
     */
    @GetMapping("/address/list")
    public CommonResult fetchAddressList(HttpServletRequest request){
        // 从请求头中获取token
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        int memberId = memberMapper.member_id(userName);
        List<UmsMemberReceiveAddress> memberReceiveAddressList = umsMemberReceiveAddressService.selectByMemberId(memberId);
        return CommonResult.success(memberReceiveAddressList);
    }

    /**
        删除
     * @param id
     * @return
     */
    @PostMapping("/address/delete/{id}")
    public CommonResult deleteAddress(@PathVariable int id){
        if (umsMemberReceiveAddressService.delete(id) > 0){
            return CommonResult.success("删除成功!");
        }
        return CommonResult.failed("删除失败!");
    }

    /**
        添加收货地址
     * @param umsMemberReceiveAddress
     * @param request
     * @return
     */
    @PostMapping("/address/add")
    public CommonResult addAddress(@RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress,HttpServletRequest request){
        // 从请求头中获取token
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        int memberId = memberMapper.member_id(userName);
        umsMemberReceiveAddress.setMemberId(memberId);
        if (umsMemberReceiveAddressService.add(umsMemberReceiveAddress) > 0){
            return CommonResult.success("添加收货地址成功!");
        }
        return CommonResult.failed("添加收货地址失败!");
    }

    /**
        编辑回显
     * @param id
     * @return
     */
    @GetMapping("/address/{id}")
    public CommonResult fetchAddressDetail(@PathVariable int id){
        UmsMemberReceiveAddress umsMemberReceiveAddress = umsMemberReceiveAddressService.fetchAddressDetail(id);
        if (umsMemberReceiveAddress != null){
            return CommonResult.success(umsMemberReceiveAddress);
        }
        return CommonResult.failed("回显失败!");
    }

    @PostMapping("address/update/{id}")
    public CommonResult updateAddress(@PathVariable int id,@RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress){
       if (umsMemberReceiveAddressService.updateAddress(umsMemberReceiveAddress) > 0){
           return CommonResult.success("编辑成功!");
       }
        return CommonResult.failed("编辑失败!");
    }
}
